package vh.android.multiconnector.quiz.ui.adapters;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import vh.android.multiconnector.library.model.Channel;
import vh.android.multiconnector.library.model.Member;
import vh.android.multiconnector.quiz.MulticastService;
import vh.android.multiconnector.quiz.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class ChannelAdapter extends BaseAdapter {

	private ArrayList<Channel> mChannels = new ArrayList<Channel>();
	private Context mContext;

	public ChannelAdapter(Context pContext) {
		mContext = pContext;
	}

	@Override
	public int getCount() {
		return mChannels.size();
	}

	@Override
	public Channel getItem(int pIndex) {
		return mChannels.get(pIndex);
	}

	@Override
	public long getItemId(int pIndex) {
		return pIndex;
	}

	@Override
	public View getView(int pIndex, View pView, ViewGroup pViewGroup) {
		Channel channel = getItem(pIndex);

		if (pView == null) {
			LayoutInflater inflater = LayoutInflater.from(mContext);
			pView = inflater.inflate(R.layout.row_channel, null);
		}

		TextView tvChannelName = (TextView) pView
				.findViewById(R.id.tvChannelName);
		TextView tvPlayerCount = (TextView) pView
				.findViewById(R.id.tvPlayerCount);
		// TextView tvCreater = (TextView) pView.findViewById(R.id.tvCreater);
		// TextView tvStatus = (TextView) pView.findViewById(R.id.tvStatus);

		tvChannelName.setText(channel.getName());
		// TODO
		// tvCreater.setText(channel.getCreaterMac());

		List<Member> members = MulticastService.getInstance().getMembers(
				channel);
		int playerCount = members.size();
		tvPlayerCount.setText(String.format("(%d Spieler)", playerCount));

		// if (channel.hasStarted()) {
		// tvStatus.setTextColor(Color.RED);
		// tvStatus.setText("geschlossen");
		// } else {
		// tvStatus.setTextColor(Color.GREEN);
		// tvStatus.setText("offen");
		// }
		return pView;
	}

	public void addAll(Collection<Channel> channels) {
		mChannels.addAll(channels);
		notifyDataSetChanged();
	}

}
